﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
* Validation settings
*
* There is an action on every EntityController/ValidationRules.
* It returns the list of rules which should be applied during editing of the entity.
*
* By default it is triggered during the OnBeforeEdit() and OnBeforeNew().
*
* EntityRules declared as
* (f, entity) => f.CheckLength(() => entity.Login, 1, 50) // Text
* (f, entity) => f.CheckValue(() => entity.Value, -100, 100) // ValueType
*
* are parsed and converted to jquery.validator rules automatically
* (e.g. ..User.mvc/ValidationRules/cs-CZ?filter=Add)
*
*/


/* Validator */
var validator = { rules: [], messages: [] };
var validatorMessages = { };
$(document).ready(function ()
{
    $("#" + _mainForm).validate(validator);
});
$.validator.setDefaults({
    errorElement: "kbd"
});
var _inputMaxLengthBound = 5000;
var _inputMaxLength = 4000;
/* Validator: MaxLength attribute, culture dependent value checks */
$(document).ready(function ()
{
    for (var name in validator.rules)
    {
        var rule = validator.rules[name];
        if (rule.maxlength != null)
        {   // max length for not set, following the rules
            $("#" + _mainForm + " :text[name='" + name + "']" +
             ",#" + _mainForm + " TEXTAREA[name='" + name + "']").each(function (e)
            {
                var maxLength = $(this).attr("maxlength");
                if (maxLength == null || maxLength < 0 || maxLength > _inputMaxLengthBound)
                {
                    $(this).attr("maxlength", rule.maxlength);
                }
            });
        }
    };

    $.validator.addMethod("min", function (value, element, param)
    {
        var num = value.replace(RegExp(" ", "g"), "") // remove spaces
                       .replace(RegExp('\\' + defaultThousandSeprator, "g"), "") // cultrue thousand seprator
                       .replace(RegExp("\\" + defaultDecimalSeprator, "g"), "."); // fix decimals
        return this.optional(element) || num >= param;
    });
    $.validator.addMethod("max", function (value, element, param)
    {
        var num = value.replace(RegExp(" ", "g"), "") // remove spaces
                       .replace(RegExp('\\' + defaultThousandSeprator, "g"), "") // cultrue thousand seprator
                       .replace(RegExp("\\" + defaultDecimalSeprator, "g"), "."); // fix decimals
        return this.optional(element) || num <= param;
    });
});